package it.apiconsulting.editT.view;

import it.apiconsulting.editT.controller.UtenteManager;
import it.apiconsulting.editT.model.Utente;
import it.apiconsulting.editT.session.SessionBean;
import it.apiconsulting.editT.util.CipherUtils;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 9149826260758390091L;
	private String username;
	private String password;
	
	@Autowired
	@Qualifier("sessionBean")
	private SessionBean session;
	
	private List<Utente> utenti;
	private Long id;

	private UtenteManager linkController;

	public LoginAction() {
		linkController = new UtenteManager();
	}

	public String execute() {
		boolean find = false;
		this.utenti = linkController.list();
		for (int i = 0; i < utenti.size(); i++) {
			String pwdDb = CipherUtils.decrypt(utenti.get(i).getPassword());
			if (utenti.get(i).getUsername().equals(username)
					&& pwdDb.equals(password))
				find = true;
			if (find)
				break;
		}
		if (find)
			return SUCCESS;
		else
			return ERROR;
	}
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
}